package com.ceshiren.mini.controller;

import com.ceshiren.mini.dto.TestCaseDTO;
import com.ceshiren.mini.service.TestCaseService;
import com.ceshiren.mini.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

import static com.ceshiren.mini.util.ResultCode.*;

@RestController
@RequestMapping("testcase")
@Api(tags = "测试用例管理")
public class TestCaseController {

    private TestCaseService testCaseService;

    @Autowired
    public void setTestCaseService(TestCaseService testCaseService) {
        this.testCaseService = testCaseService;
    }

    //创建测试用例
    @ApiOperation(value = "新增测试用例")
    @PostMapping
    public R createTestCase(@RequestBody TestCaseDTO testCaseDTO){
        System.out.println("测试用例的新增：" + testCaseDTO);

        //参数是否为空进行判断
        if(Objects.isNull(testCaseDTO)){
            return R.error(PARAMETER_ERROR).message("测试用例为空");
        }
        //caseName  不能为空
        if(!StringUtils.hasText(testCaseDTO.getName())){
            return R.error(PARAMETER_ERROR).message("测试用例名称不能为空");
        }

        //传入参数正常，可以添加测试用例
        int insertTestCaseNum = testCaseService.createTestCase(testCaseDTO);
        if(insertTestCaseNum == 1){
            testCaseDTO = testCaseService.getTestCase(testCaseDTO);
        }
        return insertTestCaseNum == 1
                ? R.ok().data(testCaseDTO).message(insertTestCaseNum + "条测试用例添加成功")
                : R.error(ADDERROR).message("测试用例已存在");
    }

    //获取测试用例，若ID存在则查看具体的测试用例信息；如果ID不存在则查看所有的测试用例
    @ApiOperation(value = "测试用例查找")
    @GetMapping
    public R getOneTestCase(@RequestParam(name = "id", required = false) Integer caseId){
        if(null == caseId){
            List<TestCaseDTO> testCaseDtoList = testCaseService.getTestCaseList();
            //返回的实体类如果是空，null  error
            return (testCaseDtoList == null || testCaseDtoList.isEmpty())
                    ? R.error(PARAMETER_NOT_EXIST).message("没有测试用例")
                    : R.ok().data(testCaseDtoList);
        }else {
            TestCaseDTO testCaseDTO = new TestCaseDTO();
            testCaseDTO.setId(caseId);
            TestCaseDTO testCaseDto = testCaseService.getTestCase(testCaseDTO);
            //返回的实体类如果是空，null  error
            return Objects.isNull(testCaseDto)
                    ? R.error(PARAMETER_NOT_EXIST).message("用例不存在")
                    : R.ok().data(testCaseDto);
        }
    }

    //删除测试用例
    @ApiOperation(value = "删除测试用例")
    @DeleteMapping
    public R deleteTestCase(@RequestBody TestCaseDTO testCaseDTO){
        int deleted = testCaseService.deleteTestCase(testCaseDTO);
        return deleted < 0
                ? R.error(DELETEERROR)
                : R.ok().data(testCaseDTO.getId());
    }

    //修改测试用例
    @ApiOperation(value = "更新测试用例")
    @PutMapping
    public R updateTestCase(@RequestBody TestCaseDTO testCaseDTO){
        int update = testCaseService.updateTestCase(testCaseDTO);
        return update == 1
                ? R.ok().data(testCaseDTO).message(update + "条测试用例更新成功")
                : R.error(UPDATERROR);
    }

}
